import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { GetAccessTokenDto } from './dto/get-access-token.dto';
import { GetPhoneNumberDto, GetPhoneNumberQuery } from './dto/get-phone-number.dto';
import { GetCodeSessionDto } from './dto/get-code-session.dto'

@Injectable()
export class WxapiService {

  async getAccessToken(query: GetAccessTokenDto) {
    const res = await axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=${query.grant_type}&appid=${query.appid}&secret=${query.secret}`)
    return res.data
  }

  async getPhoneNumber(query: GetPhoneNumberQuery, dto: GetPhoneNumberDto) {
    const res = await axios.post(`https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${query.access_token}`, dto)
    return res.data
  }

  async getCodeSession(dto: GetCodeSessionDto) {
    const res = await axios.get(`https://api.weixin.qq.com/sns/jscode2session?appid=${dto.appid}&secret=${dto.secret}&js_code=${dto.js_code}&grant_type=${dto.grant_type}`)
    return res.data
  }
}
